package com.oneonefive.trainticketsystem.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oneonefive.trainticketsystem.DTO.UserDTO;
import com.oneonefive.trainticketsystem.mapper.UserMapper;
import com.oneonefive.trainticketsystem.pojo.User;
import com.oneonefive.trainticketsystem.service.UserService;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {
    final
    UserMapper userMapper;

    public UserServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public int getRowCount(Integer userId, String userName) {
        return userMapper.getRowCount(userId, userName);
    }

    @Override
    public List<User> getAllUser(Integer userId, String userName, int rowCount) {
        return userMapper.selectAllUser(userId, userName, rowCount);
    }

    @Override
    public UserDTO loginCheck(User user, HttpSession session) {
        if (user == null) {
            return null;
        }
        if (user.getUserPassword().length() == 0) {
            return null;
        }
        User user1 = userMapper.selectById(user.getUserId());
        if (user1 == null) {
            return null;
        }
        if (!user.getUserPassword().equals(user1.getUserPassword())) {
            return null;
        }
        UserDTO userDTO = BeanUtil.copyProperties(user1, UserDTO.class);
        session.setAttribute("user", userDTO);
        return userDTO;
    }
}




